using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using AuthService.Domain.OAuth;

namespace AuthService.Application.OAuth;

/// <summary>
/// OAuth授权仓储接口
/// </summary>
public interface IOAuthAuthorizationRepository : IRepository<OAuthAuthorization, Guid>
{
    /// <summary>
    /// 根据授权码获取授权
    /// </summary>
    Task<OAuthAuthorization> GetByAuthorizationCodeAsync(
        string authorizationCode,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据用户ID获取授权
    /// </summary>
    Task<List<OAuthAuthorization>> GetByUserIdAsync(
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据客户端ID获取授权
    /// </summary>
    Task<List<OAuthAuthorization>> GetByClientIdAsync(
        string clientId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据用户ID和客户端ID获取授权
    /// </summary>
    Task<List<OAuthAuthorization>> GetByUserIdAndClientIdAsync(
        Guid userId,
        string clientId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取过期的授权
    /// </summary>
    Task<List<OAuthAuthorization>> GetExpiredAuthorizationsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取已使用的授权
    /// </summary>
    Task<List<OAuthAuthorization>> GetUsedAuthorizationsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取已撤销的授权
    /// </summary>
    Task<List<OAuthAuthorization>> GetRevokedAuthorizationsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 撤销用户的所有授权
    /// </summary>
    Task RevokeAllAuthorizationsByUserIdAsync(
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 撤销客户端的所有授权
    /// </summary>
    Task RevokeAllAuthorizationsByClientIdAsync(
        string clientId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 清理过期授权
    /// </summary>
    Task<int> CleanupExpiredAuthorizationsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取授权统计
    /// </summary>
    Task<OAuthAuthorizationStatisticsDto> GetAuthorizationStatisticsAsync(
        Guid? userId = null,
        string clientId = null,
        CancellationToken cancellationToken = default);
}

/// <summary>
/// OAuth客户端仓储接口
/// </summary>
public interface IOAuthClientRepository : IRepository<OAuthClient, Guid>
{
    /// <summary>
    /// 根据客户端ID获取客户端
    /// </summary>
    Task<OAuthClient> GetByClientIdAsync(
        string clientId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取启用的客户端
    /// </summary>
    Task<List<OAuthClient>> GetEnabledClientsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据授权类型获取客户端
    /// </summary>
    Task<List<OAuthClient>> GetClientsByGrantTypeAsync(
        string grantType,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据作用域获取客户端
    /// </summary>
    Task<List<OAuthClient>> GetClientsByScopeAsync(
        string scope,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 检查客户端是否存在
    /// </summary>
    Task<bool> ExistsByClientIdAsync(
        string clientId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 验证客户端密钥
    /// </summary>
    Task<bool> ValidateClientSecretAsync(
        string clientId,
        string clientSecret,
        CancellationToken cancellationToken = default);
}

/// <summary>
/// OAuth授权统计DTO
/// </summary>
public class OAuthAuthorizationStatisticsDto
{
    /// <summary>
    /// 总授权数
    /// </summary>
    public int TotalAuthorizations { get; set; }

    /// <summary>
    /// 活跃授权数
    /// </summary>
    public int ActiveAuthorizations { get; set; }

    /// <summary>
    /// 过期授权数
    /// </summary>
    public int ExpiredAuthorizations { get; set; }

    /// <summary>
    /// 已使用授权数
    /// </summary>
    public int UsedAuthorizations { get; set; }

    /// <summary>
    /// 已撤销授权数
    /// </summary>
    public int RevokedAuthorizations { get; set; }

    /// <summary>
    /// 按类型分组统计
    /// </summary>
    public Dictionary<string, int> AuthorizationsByType { get; set; } = new();

    /// <summary>
    /// 按客户端分组统计
    /// </summary>
    public Dictionary<string, int> AuthorizationsByClient { get; set; } = new();
}